<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/30 0030
 * Time: 16:47
 */

namespace app\api\model;


class Theme extends BaseModel
{
    protected $hidden = ['topic_img_id','delete_time','head_img_id','update_time'];
    public function topicImg(){
//        一对一(是不对等的)
//        $this->hasOne();放在Image模型里（Image表没有外键）
        return $this->belongsTo('Image','topic_img_id','id');//一对一（theme表有外键）
    }
    public function headImg(){
        return $this->belongsTo('Image','head_img_id','id');
    }
    public function products(){
//        多对多
        return $this->belongsToMany('Product','theme_product','product_id','theme_id');
    }
    public static function getThemeWithProducts($id){
//        with 相当于inner join
        $theme = self::with('products,topicImg,headImg')
            ->find($id);
        return $theme;
    }
}